package com.kaishu.misp.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishu.misp.entity.ProductType;
import com.kaishu.misp.service.ProductTypeService;
import com.kaishu.misp.utils.ExceptionUtil;
import com.kaishu.misp.action.Json;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class ProductTypeAction extends BaseAction implements ModelDriven<ProductType>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(ProductTypeAction.class);
	
	@Autowired
	private ProductTypeService productTypeService;
	public ProductType productType = new ProductType();
	
	/**
	 * 得到数据列表
	 */
	@Action("productTypeGrid")
	public void grid(){
		int total = productTypeService.getTotal();
		List<ProductType> list = productTypeService.findAll(page, rows, sort, order);		
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	@Action("productTypeAdd")
	public void add(){
			Json result = new Json();
			try {
				productTypeService.save(productType);
				result.setSuccess(true);
				result.setMsg("添加产品类别成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加产品类别失败qq");
				System.out.println(ExceptionUtil.getExceptionMessage(e));
			}
			System.out.println(productType.getDescription());
			System.out.println(productType.getProducttypename());
			System.out.println(productType.getId());
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	@Action("productTypeEdit")
	public void edit() {
			Json result = new Json();
			try {
				productTypeService.update(productType);
				result.setSuccess(true);
				result.setMsg("修改产品类别成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加产品类别失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	@Action("productTypeRemove")
	public void remove() {
			Json result = new Json();
			try {
				productTypeService.delete(productType.getId());
				result.setSuccess(true);
				result.setMsg("删除产品类别成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除产品类别失败");
			}
			writeJson(result);
	}
	
	@Override
	public ProductType getModel() {
		return productType;
	}

}
